package ustc.xiaoyang.karl.slidingwindows;

/**
 * 209.长度最小的子数组
 */
public class TheSubarrayWithTheSmallestLength209 {
    public static void main(String[] args) {

        int [] nums = {2,3,1,2,4,3};
        System.out.println(new TheSubarrayWithTheSmallestLength209().minSubArrayLen(7, nums));
    }
    public int minSubArrayLen(int target,int[] nums){
        int sum = 0;
        int i = 0;
        int res = Integer.MAX_VALUE;
        for (int j = 0; j < nums.length; j++) {
            sum += nums[j];
            while(sum >= target){
                int size = j - i + 1;
                res = Math.min(res,size);
                sum -= nums[i++];
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}
